﻿using AssistCore;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Text;
using System.Linq;
using System.Threading;
using System.Windows.Forms;

namespace Helper
{
    public partial class KeyHelper : CCWin.Skin_Mac
    {
        public KeyHelper()
        {
            InitializeComponent();
            this.Icon = Properties.HelpResource.app;
        }

        private DateTime lastComboKeyTime = DateTime.Now;
        private HashSet<Keys> waitForUpKeys = new HashSet<Keys>();
        private BindingList<ComboKeyData> combokeyData = new BindingList<ComboKeyData>();
        private IntPtr seriesHwnd = IntPtr.Zero, comboHwnd = IntPtr.Zero;

        private KeyModifiers seriesModifyKey = KeyModifiers.None, triggerModifyKey = KeyModifiers.None;
        private Keys seriesHotKey = Keys.None, triggerHotKey = Keys.None;

        private KeyModifiers comboTriggerModifyKey = KeyModifiers.None;
        private Keys comboTriggerHotKey = Keys.None;

        private Thread seriesThread = null, comboThread = null, keyDownThread = null;

        private void ComboTextKeyDown(object sender, KeyEventArgs e)
        {
            if (cb_comboDisable.Checked)
            {
                return;
            }
            if (e.KeyCode == Keys.ShiftKey || e.KeyCode == Keys.Menu || e.KeyCode == Keys.ControlKey)
            {
                return;
            }
            var comboModifyKey = KeyModifiers.None;
            var key = e.KeyCode.ToString();
            var sb = new StringBuilder();
            if (e.Control)
            {
                sb.Append("Ctrl+");
                comboModifyKey = comboModifyKey | KeyModifiers.Control;
            }
            if (e.Shift)
            {
                sb.Append("Shift+");
                comboModifyKey = comboModifyKey | KeyModifiers.Shift;
            }
            if (e.Alt)
            {
                sb.Append("Alt+");
                comboModifyKey = comboModifyKey | KeyModifiers.Alt;
            }
            sb.Append(key);
            ((TextBox)sender).Text = sb.ToString();
        }

        private void txb_seriesKey_KeyDown(object sender, KeyEventArgs e)
        {
            if (cb_seriesDisable.Checked)
            {
                return;
            }
            KeyChanged(sender, e, ref seriesModifyKey, ref seriesHotKey);
        }
        private void txb_trigger_key_KeyDown(object sender, KeyEventArgs e)
        {
            if (cb_seriesDisable.Checked)
            {
                return;
            }
            KeyChanged(sender, e, ref triggerModifyKey, ref triggerHotKey);
        }
        private void txb_trigger_combo_key_KeyDown(object sender, KeyEventArgs e)
        {
            if (cb_seriesDisable.Checked)
            {
                return;
            }
            KeyChanged(sender, e, ref comboTriggerModifyKey, ref comboTriggerHotKey);
        }
        private void ClearTextKeys(object sender, EventArgs e)
        {
            TextBox txb = (TextBox)sender;
            txb.Text = "";
            if (txb.Name == txb_seriesKey.Name)
            {
                seriesHotKey = Keys.None;
                seriesModifyKey = KeyModifiers.None;
            }
            else if (txb.Name == txb_trigger_series_key.Name)
            {
                triggerHotKey = Keys.None;
                triggerModifyKey = KeyModifiers.None;
            }
            else if (txb.Name == txb_trigger_combo_key.Name)
            {
                comboTriggerHotKey = Keys.None;
                comboTriggerModifyKey = KeyModifiers.None;
            }
        }

        private void KeyChanged(object sender, KeyEventArgs e, ref KeyModifiers modifyKey, ref Keys hotKey)
        {
            if (e.KeyCode == Keys.ShiftKey || e.KeyCode == Keys.Menu || e.KeyCode == Keys.ControlKey)
            {
                return;
            }
            var key = e.KeyCode.ToString();
            var sb = new StringBuilder();
            if (e.Control)
            {
                sb.Append("Ctrl+");
                modifyKey = modifyKey | KeyModifiers.Control;
            }
            if (e.Shift)
            {
                sb.Append("Shift+");
                modifyKey = modifyKey | KeyModifiers.Shift;
            }
            if (e.Alt)
            {
                sb.Append("Alt+");
                modifyKey = modifyKey | KeyModifiers.Alt;
            }
            sb.Append(key);
            ((TextBox)sender).Text = sb.ToString();
            hotKey = e.KeyCode;
        }
        private void KeyHelper_FormClosed(object sender, FormClosedEventArgs e)
        {
            StopSeriesKey();
            StopComboKey();

            Properties.HelperSettings.Default.seriesModifyKey = this.seriesModifyKey;
            Properties.HelperSettings.Default.seriesHotKey = this.seriesHotKey;
            Properties.HelperSettings.Default.numSeriesDelay = this.num_series_delay.Value;
            Properties.HelperSettings.Default.triggerModifyKey = this.triggerModifyKey;
            Properties.HelperSettings.Default.triggerHotKey = this.triggerHotKey;
            Properties.HelperSettings.Default.lockSeriesWindow = this.cb_lockSeriesWindow.Checked;
            Properties.HelperSettings.Default.comboTriggerModifyKey = this.comboTriggerModifyKey;
            Properties.HelperSettings.Default.comboTriggerHotKey = this.comboTriggerHotKey;
            Properties.HelperSettings.Default.combokeyData = this.combokeyData.ToArray();
            Properties.HelperSettings.Default.comboKeyDelay = this.num_comboKeyDelay.Value;
            Properties.HelperSettings.Default.lockComboWindow = this.cb_lockComboWindow.Checked;
            Properties.HelperSettings.Default.comboLoop = this.cb_combo_loop.Checked;
            Properties.HelperSettings.Default.keyDownMode = this.cb_keyDownMode.Checked;
            Properties.HelperSettings.Default.Save();
            
            Win32Core.UnregisterHotKey(this.Handle, (int)HotKeyTypes.SeriesKey);
            Win32Core.UnregisterHotKey(this.Handle, (int)HotKeyTypes.ComboKey);
        }
        private void KeyHelper_Load(object sender, EventArgs e)
        {
            this.seriesModifyKey = Properties.HelperSettings.Default.seriesModifyKey;
            this.seriesHotKey = Properties.HelperSettings.Default.seriesHotKey;
            this.num_series_delay.Value = Properties.HelperSettings.Default.numSeriesDelay;
            this.triggerModifyKey = Properties.HelperSettings.Default.triggerModifyKey;
            this.triggerHotKey = Properties.HelperSettings.Default.triggerHotKey;
            this.cb_lockSeriesWindow.Checked = Properties.HelperSettings.Default.lockSeriesWindow;
            this.comboTriggerModifyKey = Properties.HelperSettings.Default.comboTriggerModifyKey;
            this.comboTriggerHotKey = Properties.HelperSettings.Default.comboTriggerHotKey;
            this.cb_keyDownMode.Checked = Properties.HelperSettings.Default.keyDownMode;
            this.txb_trigger_series_key.Enabled = !this.cb_keyDownMode.Checked;
            if (Properties.HelperSettings.Default.combokeyData == null)
            {
                this.combokeyData = new BindingList<ComboKeyData>();
            }
            else
            {
                this.combokeyData = new BindingList<ComboKeyData>(Properties.HelperSettings.Default.combokeyData.ToList());
            }
            this.num_comboKeyDelay.Value = Properties.HelperSettings.Default.comboKeyDelay;
            this.cb_lockComboWindow.Checked = Properties.HelperSettings.Default.lockComboWindow;
            this.cb_combo_loop.Checked = Properties.HelperSettings.Default.comboLoop;
            var seriesKeyString = new StringBuilder();
            if (this.seriesModifyKey.HasFlag(KeyModifiers.Control))
            {
                seriesKeyString.Append("Ctrl+");
            }
            if (this.seriesModifyKey.HasFlag(KeyModifiers.Shift))
            {
                seriesKeyString.Append("Shift+");
            }
            if (this.seriesModifyKey.HasFlag(KeyModifiers.Alt))
            {
                seriesKeyString.Append("Alt+");
            }
            if (this.seriesHotKey != Keys.None)
            {
                this.txb_seriesKey.Text = seriesKeyString.ToString() + this.seriesHotKey.ToString();
            }

            var triggerSeriesKeyString = new StringBuilder();
            if (this.triggerModifyKey.HasFlag(KeyModifiers.Control))
            {
                triggerSeriesKeyString.Append("Ctrl+");
            }
            if (this.triggerModifyKey.HasFlag(KeyModifiers.Shift))
            {
                triggerSeriesKeyString.Append("Shift+");
            }
            if (this.triggerModifyKey.HasFlag(KeyModifiers.Alt))
            {
                triggerSeriesKeyString.Append("Alt+");
            }
            if (this.triggerHotKey != Keys.None)
            {
                this.txb_trigger_series_key.Text = triggerSeriesKeyString.ToString() + this.triggerHotKey.ToString();
            }

            var triggerComboKeyString = new StringBuilder();
            if (this.comboTriggerModifyKey.HasFlag(KeyModifiers.Control))
            {
                triggerComboKeyString.Append("Ctrl+");
            }
            if (this.comboTriggerModifyKey.HasFlag(KeyModifiers.Shift))
            {
                triggerComboKeyString.Append("Shift+");
            }
            if (this.comboTriggerModifyKey.HasFlag(KeyModifiers.Alt))
            {
                triggerComboKeyString.Append("Alt+");
            }
            if (this.comboTriggerHotKey != Keys.None)
            {
                this.txb_trigger_combo_key.Text = triggerComboKeyString + this.comboTriggerHotKey.ToString();
            }
            if (combokeyData.Count > 0)
            {
                listComboKeys.DataSource = combokeyData;
            }
        }

        private void cb_seriesDisable_CheckedChanged(object sender, EventArgs e)
        {
            if (cb_seriesDisable.Checked)
            {

                if (seriesHotKey == Keys.None || triggerHotKey == Keys.None)
                {
                    MessageBox.Show("还未设置连发键或触发键！", "悟空你又调皮了", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    cb_seriesDisable.Checked = false;
                    return;
                }
                if (seriesHotKey == comboTriggerHotKey && seriesModifyKey == comboTriggerModifyKey)
                {
                    MessageBox.Show("启动键设置冲突！请设置不同的启动键！", "悟空你又调皮了", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    cb_seriesDisable.Checked = false;
                    return;
                }
                if (cb_keyDownMode.Checked)
                {
                    keyDownThread = new Thread(() =>
                    {
                        while (true)
                        {
                            var keyState = Win32Core.GetKeyState((int)seriesHotKey);
                            if (keyState < 0)
                            {
                                var ddModifyKey = DDKeys.None;
                                if (seriesModifyKey != KeyModifiers.None)
                                {
                                    ddModifyKey = KeyCodeMap.ModifyKeyToDD(seriesModifyKey);
                                    CDD.DDContext.key((int)ddModifyKey, 1);
                                }
                                var ddKey = KeyCodeMap.KeysToDD(seriesHotKey);
                                CDD.DDContext.key((int)ddKey, 1);
                                CDD.DDContext.key((int)ddKey, 2);
                                if (seriesModifyKey != KeyModifiers.None)
                                {
                                    CDD.DDContext.key((int)ddModifyKey, 2);
                                }
                                Thread.Sleep((int)num_series_delay.Value);
                            }
                            Console.WriteLine(keyState);
                        }
                    });
                    keyDownThread.Start();
                }
                else
                {
                    Win32Core.RegisterHotKey(this.Handle, (int)HotKeyTypes.SeriesKey, (int)triggerModifyKey, (int)triggerHotKey);
                }
            }
            else
            {
                Win32Core.UnregisterHotKey(this.Handle, (int)HotKeyTypes.SeriesKey);
                StopSeriesKey();
            }
        }
        private void cb_comboDisable_CheckedChanged(object sender, EventArgs e)
        {
            if (cb_comboDisable.Checked)
            {
                if (comboTriggerHotKey == Keys.None || combokeyData.Count == 0)
                {
                    MessageBox.Show("还未设置触发键或者任何一个连续按键！", "悟空你又调皮了", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    cb_comboDisable.Checked = false;
                    return;
                }
                if (seriesHotKey == comboTriggerHotKey && seriesModifyKey == comboTriggerModifyKey)
                {
                    MessageBox.Show("启动键设置冲突！请设置不同的启动键！", "悟空你又调皮了", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    cb_comboDisable.Checked = false;
                    return;
                }
                Win32Core.RegisterHotKey(this.Handle, (int)HotKeyTypes.ComboKey, (int)comboTriggerModifyKey, (int)comboTriggerHotKey);
            }
            else
            {
                Win32Core.UnregisterHotKey(this.Handle, (int)HotKeyTypes.ComboKey);
                StopComboKey();
            }
        }
        protected override void WndProc(ref Message m)
        {
            const int WM_HOTKEY = 0x0312;
            switch (m.Msg)
            {
                case WM_HOTKEY:
                    HandleHotkey(m);
                    break;
            }
            base.WndProc(ref m);
        }
        private void HandleHotkey(Message m)
        {
            switch (m.WParam.ToInt32())
            {
                case (int)HotKeyTypes.SeriesKey:
                    {
                        SeriesKey();
                        break;
                    }
                case (int)HotKeyTypes.ComboKey:
                    {
                        ComboKey();
                        break;
                    }
            }
        }

        private void RecordComboKeyKeyDown(object sender, KeyEventArgs e)
        {
            var handled = HandleKeyEvent(e.KeyCode, KeyEventType.KeyDown);
            e.Handled = handled;
            if (!handled)
            {
                AddComboKeys(KeyEventType.KeyDown, e.KeyCode);
            }
        }
        private void RecordComboKeyKeyUp(object sender, KeyEventArgs e)
        {
            var handled = HandleKeyEvent(e.KeyCode, KeyEventType.KeyUp);
            e.Handled = handled;
            if (!handled)
            {
                AddComboKeys(KeyEventType.KeyUp, e.KeyCode);
            }
        }

        void SeriesKey()
        {
            if (seriesThread != null && seriesThread.IsAlive)
            {
                StopSeriesKey();
            }
            else
            {
                var p = new Point();
                Win32Core.GetCursorPos(out p);
                seriesHwnd = Win32Core.WindowFromPoint(p);
                seriesThread = new Thread(() =>
                {
                    Thread.Sleep(300);
                    while (true)
                    {
                        if (cb_lockSeriesWindow.Checked)
                        {
                            Win32Core.SetForegroundWindow(seriesHwnd);
                        }
                        else
                        {
                            if (Win32Core.GetForegroundWindow() != seriesHwnd)
                            {
                                Thread.Sleep((int)num_series_delay.Value);
                                continue;
                            }
                        }
                        var ddModifyKey = DDKeys.None;
                        if (seriesModifyKey != KeyModifiers.None)
                        {
                            ddModifyKey = KeyCodeMap.ModifyKeyToDD(seriesModifyKey);
                            CDD.DDContext.key((int)ddModifyKey, 1);
                        }
                        var ddKey = KeyCodeMap.KeysToDD(seriesHotKey);
                        CDD.DDContext.key((int)ddKey, 1);
                        CDD.DDContext.key((int)ddKey, 2);
                        if (seriesModifyKey != KeyModifiers.None)
                        {
                            CDD.DDContext.key((int)ddModifyKey, 2);
                        }
                        Thread.Sleep((int)num_series_delay.Value);
                    }
                });
                seriesThread.Start();
            }
        }
        void StopSeriesKey()
        {
            if (seriesThread != null && seriesThread.IsAlive)
            {
                seriesThread.Abort();
            }
            if (keyDownThread != null && keyDownThread.IsAlive)
            {
                keyDownThread.Abort();
            }
        }

        void ComboKey()
        {
            if (comboThread != null && comboThread.IsAlive)
            {
                StopComboKey();
            }
            else
            {
                var p = new System.Drawing.Point();
                Win32Core.GetCursorPos(out p);
                comboHwnd = Win32Core.WindowFromPoint(p);
                comboThread = new Thread(() =>
                {
                    do
                    {
                        if (cb_lockComboWindow.Checked)
                        {
                            Win32Core.SetForegroundWindow(comboHwnd);
                        }
                        else
                        {
                            if (Win32Core.GetForegroundWindow() != comboHwnd)
                            {
                                MessageBox.Show("由于您没有勾选锁定窗口并且窗口发生了变化，因此按键将停止");
                                StopComboKey();
                            }
                        }
                        for (var i = 0; i < combokeyData.Count; i++)
                        {
                            var keyData = combokeyData[i];
                            if (keyData.Delay >= 0)
                            {
                                Thread.Sleep(keyData.Delay);
                            }
                            else
                            {
                                if (keyData.EventType == KeyEventType.KeyDown || keyData.EventType == KeyEventType.KeyUp)
                                {
                                    var ddKey = KeyCodeMap.KeysToDD(keyData.KeyCode);
                                    CDD.DDContext.key((int)ddKey, (int)keyData.EventType);
                                }
                                else if (keyData.EventType == KeyEventType.LeftClick)
                                {
                                    CDD.DDContext.btn(1);
                                    CDD.DDContext.btn(2);
                                }
                                else if (keyData.EventType == KeyEventType.LeftDoubleClick)
                                {
                                    CDD.DDContext.btn(1);
                                    CDD.DDContext.btn(2);
                                    Thread.Sleep(new Random().Next(5, 20));
                                    CDD.DDContext.btn(1);
                                    CDD.DDContext.btn(2);
                                }
                                else if (keyData.EventType == KeyEventType.RightClick)
                                {
                                    CDD.DDContext.btn(4);
                                    CDD.DDContext.btn(8);
                                }
                            }
                        }
                        if (num_comboKeyDelay.Value > 0 && cb_combo_loop.Checked)
                        {
                            Thread.Sleep((int)num_comboKeyDelay.Value);
                        }
                    } while (cb_combo_loop.Checked);
                });
                comboThread.Start();
            }
        }

        private void listComboKeys_DrawItem(object sender, DrawItemEventArgs e)
        {
            if (e.Index >= 0)
            {
                var backColor = e.BackColor;
                if (e.State.HasFlag(DrawItemState.Selected))
                {
                    backColor = Color.FromArgb(202, 225, 255);
                }
                e.Graphics.FillRectangle(new SolidBrush(backColor), e.Bounds);
                if (e.Index >= 0)
                {
                    var disPlayStr = ((ListBox)sender).Items[e.Index].ToString();
                    Brush mybsh = Brushes.Black;
                    if (disPlayStr.IndexOf("延迟") != -1)
                    {
                        mybsh = new SolidBrush(Color.FromArgb(128, 128, 128));
                    }
                    StringFormat sStringFormat = new StringFormat();
                    sStringFormat.LineAlignment = StringAlignment.Center;
                    e.Graphics.DrawString(disPlayStr, e.Font, mybsh, e.Bounds, sStringFormat);
                }
                e.DrawFocusRectangle();
            }
        }

        private void listComboKeys_MeasureItem(object sender, MeasureItemEventArgs e)
        {
            e.ItemHeight = e.ItemHeight + 7;
        }

        private void comboListContextMenu_Opening(object sender, System.ComponentModel.CancelEventArgs e)
        {
            if (listComboKeys.SelectedIndex < 0 || combokeyData.Count == 0)
            {
                插入动作ToolStripMenuItem.Enabled = false;
                修改延迟ToolStripMenuItem.Enabled = false;
            }
            else
            {
                var selectItem = (ComboKeyData)listComboKeys.SelectedItem;
                if (selectItem != null && selectItem.Delay > 0)
                {
                    修改延迟ToolStripMenuItem.Enabled = true;
                    插入动作ToolStripMenuItem.Enabled = false;
                }
                else
                {
                    修改延迟ToolStripMenuItem.Enabled = false;
                    插入动作ToolStripMenuItem.Enabled = true;
                }
            }
        }

        private void cb_combo_loop_CheckedChanged(object sender, EventArgs e)
        {
            if (cb_combo_loop.Checked)
            {
                num_comboKeyDelay.Enabled = true;
            }
            else
            {
                num_comboKeyDelay.Enabled = false;
            }
        }

        private void 删除全部ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("确定要删除所有动作吗？", "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Question) == DialogResult.OK)
            {
                combokeyData.Clear();
            }
        }

        void StopComboKey()
        {
            if (comboThread != null)
            {
                comboThread.Abort();
            }
        }

        private void 修改延迟ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            var item = (ComboKeyData)listComboKeys.SelectedItem;
            if (item.Delay < 0)
            {
                MessageBox.Show("这个动作不是延迟动作，无法修改", "提示", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
                return;
            }
            Box box = new Box("输入延迟毫秒数（0-30000）", item.Delay);
            box.OnInputMessage += (message) =>
            {
                int delay = 0;
                if (!int.TryParse(message, out delay))
                {
                    MessageBox.Show("请输入有效的毫秒数！", "悟空你又调皮了", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                    return;
                }
                try
                {
                    item.Delay = delay;
                }
                catch (Exception ex)
                {
                    MessageBox.Show("修改数据失败！\n" + ex.Message, "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            };
            box.ShowDialog();
        }

        private void 左键单击ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            InsertAction(KeyEventType.LeftClick);
        }

        private void 左键双击ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            InsertAction(KeyEventType.LeftDoubleClick);
        }

        private void cb_keyDownMode_CheckedChanged(object sender, EventArgs e)
        {
            txb_trigger_series_key.Enabled = !cb_keyDownMode.Checked;
        }

        private void 右键单击ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            InsertAction(KeyEventType.RightClick);
        }
        private void AddComboKeys(KeyEventType eventType, Keys keyCode)
        {
            if (listComboKeys.Items.Count > 0 && listComboKeys.Items[0].ToString() == "鼠标点击此处然后按下相应键开始录制")
            {
                listComboKeys.Items.Clear();
                listComboKeys.DataSource = combokeyData;
            }
            if (combokeyData.Count > 0)
            {
                var delay = (DateTime.Now - lastComboKeyTime).TotalMilliseconds;
                combokeyData.Add(new ComboKeyData(KeyEventType.Delay, Keys.None, Convert.ToInt32(delay)));
            }
            combokeyData.Add(new ComboKeyData(eventType, keyCode));
            listComboKeys.TopIndex = listComboKeys.Items.Count - (int)(listComboKeys.Height / (listComboKeys.ItemHeight + 7));
            lastComboKeyTime = DateTime.Now;
        }

        private bool HandleKeyEvent(Keys key, KeyEventType eventType)
        {
            if (waitForUpKeys.Contains(key))
            {
                if (eventType == KeyEventType.KeyDown)
                {
                    return true;
                }
                else
                {
                    waitForUpKeys.Remove(key);
                }
            }
            else
            {
                if (eventType == KeyEventType.KeyDown)
                {
                    waitForUpKeys.Add(key);
                }
            }
            return false;
        }
        private void InsertAction(KeyEventType eventType)
        {
            Box box = new Box("输入延迟毫秒数（0-30000）");
            box.OnInputMessage += (message) =>
            {
                int delay = 0;
                if (!int.TryParse(message, out delay))
                {
                    MessageBox.Show("请输入有效的毫秒数！", "悟空你又调皮了", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                    return;
                }
                try
                {
                    var selectItem = (ComboKeyData)listComboKeys.SelectedItem;
                    if (selectItem.EventType == KeyEventType.Delay)
                    {
                        MessageBox.Show("无法在该位置插入动作！", "悟空你又调皮了", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                        return;
                    }
                    combokeyData.Insert(listComboKeys.SelectedIndex, new ComboKeyData(eventType, Keys.LButton));
                    combokeyData.Insert(listComboKeys.SelectedIndex, new ComboKeyData(KeyEventType.Delay, Keys.None, delay));
                }
                catch (Exception ex)
                {
                    MessageBox.Show("添加数据失败！\n" + ex.Message, "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            };
            box.ShowDialog();
        }
    }
}
